﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using D3DSS.DataContract;
using System.Web.Security;
using D3DSS.Enums;
using System.Net;
using D3DSS.Domain;
using D3DSS.IoC;
using D3DSS.IDomain;
using StructureMap;
using System.Threading.Tasks;

namespace D3DSS.UnitTest
{
    class Program
    {
        static IDropStatistics dropService { get; set; }
        static IAccount accountService { get; set; }

        static void Main(string[] args)
        {
            BootStrapper.ConfigureStructureMap();
            dropService = ObjectFactory.GetInstance<IDropStatistics>();
            accountService = ObjectFactory.GetInstance<IAccount>();
            //CreateRandomData();
            //Login();
            
        }

        static void CreateRandomData()
        {
            //for (var i = 40; i < 80; i++)
            //{
            //    RegisterTest("TestAccount" + i.ToString(), "TestAccount" + i.ToString());
            //}
        }

        static void RegisterTest(string account, string password)
        {
            var url = "http://localhost:3809/Account/Register";
            var request = new RegisterRequest();
            request.Account = account;
            request.Password = FormsAuthentication.HashPasswordForStoringInConfigFile(password, "MD5");
            var result = HttpRequest.Post<Response<RegisterResponse>>(url, request);

            //for (var i = 0; i < 10; i++)
            //{
            //    CreateStatisticsOrder(result.Data.Token);
            //}
        }

        static void CreateStatisticsOrder(Token token)
        {
            Random random = new Random(DateTime.Now.Millisecond);

            var request = new StatisticsOrderRequest();
            request.Act = ActType.Act1;
            request.GameRole = GameRoleType.Host;
            request.GameServer = GameServerType.America;
            request.MF = random.Next(0, 450);
            request.RoomIP = random.Next(0, 254);
            request.UserID = token.UserID;

            var result = dropService.CreateStatisticsOrder(request);

            for (var i = 0; i < 10; i++)
            {
                CreateDrop(result.Data.StatisticsOrderID, request.MF, request.RoomIP);
            }
        }

        static void CreateDrop(int statisticsOrderID,int mf,int roomIP)
        {
            Random random = new Random(DateTime.Now.Millisecond);

            var request = new DropRequest();
            request.IsExtend = random.Next(0, 1) == 0;
            request.RoomIP = roomIP;
            request.StatisticsOrderID = statisticsOrderID;
            request.RaraDrop = random.Next(1, 4);
            request.MF = mf;
            request.Nephalem = random.Next(0, 5);

            request.TotalDrop = 5;
            request.WhiteDrop = random.Next(0, request.TotalDrop);
            request.MagicDrop = random.Next(0, request.TotalDrop - request.WhiteDrop);
            if (request.MagicDrop <= 0)
                request.MagicDrop = 0;
            request.RaraDrop = random.Next(0, request.TotalDrop - request.WhiteDrop - request.MagicDrop);
            if (request.RaraDrop <= 0)
                request.RaraDrop = 0;
            request.LegendDrop = random.Next(0, request.TotalDrop - request.WhiteDrop - request.MagicDrop - request.RaraDrop);
            if (request.LegendDrop <= 0)
                request.LegendDrop = 0;
            request.Level61RaraDrop = random.Next(0, request.RaraDrop);
            if (request.Level61RaraDrop <= 0)
                request.Level61RaraDrop = 0;
            request.Level62RaraDrop = random.Next(0, request.RaraDrop - request.Level61RaraDrop);
            if (request.Level62RaraDrop <= 0)
                request.Level62RaraDrop = 0;
            request.Level63RaraDrop = random.Next(0, request.RaraDrop - request.Level61RaraDrop - request.Level62RaraDrop);
            if (request.Level63RaraDrop <= 0)
                request.Level63RaraDrop = 0;

            var result = dropService.CreateDrop(request);
        }

        static void Login()
        {
            var request = new LoginRequest();
            request.Account = "TestAccount40";
            request.Password = FormsAuthentication.HashPasswordForStoringInConfigFile("TestAccount40", "MD5");
            var result = accountService.Login(request);
            GetStatistics(result.Data.Token);
        }

        static void GetStatistics(Token token)
        {
            var request = new QueryStatisticsRequest();
            request.Act = ActType.Act1;
            request.GameServer = GameServerType.America;
            request.UserID = token.UserID;
            var response = dropService.QueryStatistics(request);
        }
    }
}
